package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("destination")
public class DestinationController {


    @Reference
    private IDestinationService destinationService;

    @Reference
    private IRegionService regionService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private ITravelService travelService;

    @RequestMapping("")
    public String index(Model model){


        model.addAttribute("hotRegions", regionService.queryHotRegions());


        return "destination/index";
    }


    @RequestMapping("/getHotDestByRegionId")
    public String getHotDestByRegionId(Model model, Long regionId){

        //1:查询数据
        List<Destination> list = destinationService.getDestByRegionId(regionId);


        //2:拆分数据
        int size = list.size();

        //左边:leftDests
        model.addAttribute("leftDests",list.subList(0, size / 2 + 1) );

        //右边:rightDests
        model.addAttribute("rightDests",list.subList(size / 2 + 1,size ));
        //3:拼接html模板
        model.addAttribute("regionId", regionId);

        return "destination/hotdestTpl";
    }
    @RequestMapping("/guide")
    public String guide(Model model, Long id){
        List<Destination> toasts = destinationService.getToasts(id);


        //中国  广东
        Destination dest = toasts.remove(toasts.size() - 1);

        //toasts
        model.addAttribute("toasts", toasts);


        //dest  广州
        model.addAttribute("dest", dest);


        //攻略分类 同时关联查询 攻略分类下明细
        model.addAttribute("catalogs", strategyCatalogService.queryCatalogByDestId(id));

        //前3篇  strategyDetails
        List<StrategyDetail> strategyDetails = strategyDetailService.getViewNumTop3(id);
        model.addAttribute("strategyDetails",strategyDetails);





        return "destination/guide";
    }

    @RequestMapping("/surveyPage")
    public String surveyPage(Model model, @ModelAttribute("qo")StrategyCatalogQuery qo ){
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        //中国  广东
        Destination dest = toasts.remove(toasts.size() - 1);

        //toasts
        model.addAttribute("toasts", toasts);

        //dest  广州
        model.addAttribute("dest", dest);

        return "destination/survey";
    }

    @RequestMapping("/survey")
    public String survey(Model model, @ModelAttribute("qo")StrategyCatalogQuery  qo ){



        //catalogs  某个目的地下的分类集合
        List<StrategyCatalog> catalogs = strategyCatalogService.queryCatalogByDestId(qo.getDestId());
        model.addAttribute("catalogs",catalogs);

        //catalog  选中的分类
        StrategyCatalog catalog = strategyCatalogService.get(qo.getCatalogId());
        model.addAttribute("catalog",catalog);

        //detail  选中的分类下的第一个明细
        List<StrategyDetail> details = catalog.getDetails();
        if(details != null && details.size() >0){
            StrategyDetail detail = details.get(0);
            detail.setStrategyContent(strategyDetailService.getContent(detail.getId()));
            model.addAttribute("detail",detail);
        }

        return "destination/surveyTpl";
    }


    //某个目的地下游记的列表
    @RequestMapping("/travels")
    public String travelPage(Model model, @ModelAttribute("qo")TravelQuery qo){


        model.addAttribute("pageInfo", travelService.query(qo));

        return "destination/travelTpl";
    }

}
